﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace TanmiaGrp.GeneralLedger.WinApp
{
    public interface IUserMessageService
    {
        MessageBoxResult RequestUserConfirmation(string message, string caption);

        void ShowSecurityMessage(Basis.AccessDeniedSecurityException secEx);

        void ShowErrorMessage(Basis.BaseAppException appEx);

        /// <summary>
        /// Update the status bar of the MainWindow.
        /// </summary>
        /// <param name="result">Operation result.</param>
        /// <param name="succeedResource">Operation succeeded message to show.</param>
        /// <param name="unsucceedResource">Operation succeeded message to show.</param>
        /// <param name="parameters">Parameters array of the resource message.</param>
        /// <remarks>
        /// This method depend of IShellView interface.
        /// </remarks>
        void SendStatusMessage(bool result, string succeedResource, string unsucceedResource, params object[] parameters);

        /// <summary>
        /// Update the status bar of the MainWindow.
        /// </summary>
        /// <param name="resource">Resource message name to use as template.</param>
        /// <param name="parameters">Parameters array of the resource message.</param>
        /// <remarks>
        /// This method depend of IShellView interface.
        /// </remarks>
        void SendStatusMessage(string resource, params object[] parameters);

        void ShowInformationMessage(Basis.PresentationException prEx);
    }

    public class UserMessageService : IUserMessageService
    {
        #region IUserMessageService Members

        public MessageBoxResult RequestUserConfirmation(string message, string caption)
        {
            return MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.RtlReading);
        }

        public void ShowSecurityMessage(Basis.AccessDeniedSecurityException secEx)
        {
            MessageBox.Show(secEx.Message, "لا توجد صلاحية");
        }

        public void ShowErrorMessage(Basis.BaseAppException appEx)
        {
            MessageBox.Show(appEx.Message, "خطأ", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public void SendStatusMessage(bool result, string succeedResource, string unsucceedResource, params object[] parameters)
        {
            if (result)
                Application.Current.MainWindow.Title = string.Format(CultureInfo.CurrentCulture, succeedResource, parameters);
            else
                Application.Current.MainWindow.Title = string.Format(CultureInfo.CurrentCulture, unsucceedResource, parameters);
        }

        public void SendStatusMessage(string resource, params object[] parameters)
        {
            Application.Current.MainWindow.Title = string.Format(CultureInfo.CurrentCulture, resource, parameters);
        }

        public void ShowInformationMessage(Basis.PresentationException prEx)
        {
            MessageBox.Show(prEx.Message, "خطأ", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        #endregion
    }

}
